package monitor.match;

import config.Config;
import model.HTExtractItem;
import model.HTItem;
import utils.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by CatM on 18/2/4.
 */
public class BaseMatcher implements IMatcher{
    @Override
    public List<HTExtractItem> match(List<HTExtractItem> extractedItems) {
        if(extractedItems==null || extractedItems.size() ==0)return null;

        List<HTExtractItem> matchItems = new ArrayList<HTExtractItem>();

        for(HTExtractItem item: extractedItems){
            if(TextUtils.isEmpty(item.name)) continue;
            for(HTItem htItem: Config.htItems){
                //先匹配货物名字
                item.name = item.name.toLowerCase();
                if(!TextUtils.isEmpty(htItem.brand)) {
                    htItem.brand = htItem.brand.toLowerCase();
                }
                if(!TextUtils.isEmpty(htItem.product)) {
                    htItem.product = htItem.product.toLowerCase();
                }
                //一定要有品牌，
                if(TextUtils.notEmpty(htItem.brand)
                        && item.name.contains(htItem.brand)
                        && (TextUtils.isEmpty(htItem.product) || item.name.contains(htItem.product))){

                    if(!TextUtils.isEmpty(item.price)){
                        item.price = item.price.trim().replaceAll(" ", "");
                    }

                    //如果有金额匹配
                    if(htItem.amount != -1 ){
                        String regex = Config.REGIX_AMOUNT;
                        if(item.price.contains("元")){//有可能是约xxx元
                            regex = Config.REGIX_AMOUNT;
                        } else if(item.price.contains("￥")) {//有可能是￥xxx
                            regex = Config.REGIX_AMOUNT1;
                        }
                        Pattern p = Pattern.compile(regex);
                        Matcher matcher = p.matcher(item.price);
                        if(matcher.find()){
                            String amountStr = matcher.group();
                            if(Config.REGIX_AMOUNT1.equals(regex)){
                                amountStr = amountStr.substring(1, amountStr.length());
                            } else if(Config.REGIX_AMOUNT.equals(regex)){
                                amountStr = amountStr.substring(0, amountStr.length()-1);
                            }
                            if(Long.parseLong(amountStr) <= htItem.amount) {
                                matchItems.add(item);
                                System.out.println("找到啦！！---- ItemName=" + item.name + "; amount=" + amountStr);
                                break;
                            }
                        }
                    }

                    //如果有折扣匹配
                    if(htItem.discount != -1){
                        Pattern p = Pattern.compile(Config.REGIX_DISCOUNT);
                        Matcher matcher = p.matcher(item.price);
                        if(matcher.find()){
                            String discountStr = matcher.group();
                            discountStr = discountStr.substring(0, discountStr.length()-1);
                            if(Float.parseFloat(discountStr) <= htItem.discount) {
                                matchItems.add(item);
                                System.out.println("找到啦！！---- ItemName=" + item.name + "; discount=" + discountStr);
                                break;
                            }
                        }
                        //折扣同样有可能出现在itemName当中
                        matcher = p.matcher(item.name);
                        if(matcher.find()){
                            String discountStr = matcher.group();
                            discountStr = discountStr.substring(0, discountStr.length()-1);
                            if(Float.parseFloat(discountStr) <= htItem.discount) {
                                matchItems.add(item);
                                System.out.println("找到啦！！---- ItemName=" + item.name + "; discount=" + discountStr);
                                break;
                            }
                        }
                    }

                    if(htItem.discount == -1 && htItem.amount == -1){
                        matchItems.add(item);
                        break;
                    }
                }
            }
        }
        return matchItems;
    }
}
